package com.wryyyyy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wryyyyy.dto.PageBean;
import com.wryyyyy.dto.Result;
import com.wryyyyy.dto.UserDTO;
import com.wryyyyy.entity.Label;
import com.wryyyyy.service.ILabelService;
import com.wryyyyy.service.IUserLabelService;
import com.wryyyyy.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wryyyyy
 * @since 2023-11-12
 */
@Slf4j
@RestController
@RequestMapping("/label")
public class LabelController {
    @Resource
    private ILabelService labelService;

    @Resource
    private IUserLabelService userLabelService;

    /**
     * 查看标签详情
     * @param labelId 标签id
     * @return 操作结果
     */
    @GetMapping("/{labelId}")
    public Result queryLabelById(@PathVariable String labelId) {
        return Result.ok(labelService.getById(labelId));
    }

    /**
     * 用户选择标签
     * @param ids 标签集
     * @return 操作结果
     */
    @PostMapping("/choice")
    public Result choice(@RequestBody String[] ids) {
        UserDTO user = UserHolder.getUser();
        return userLabelService.choiceLabels(user.getId(), Arrays.asList(ids));
    }

    /**
     * 管理员添加标签
     * @param label 标签
     * @return 操作结果
     */
    @PostMapping("/save")
    public Result save(@RequestBody Label label) {
        if (!labelService.save(label)) {
            return Result.fail("添加失败-_-!");
        }
        return Result.ok();
    }

    /**
     * 删除单个标签
     * @param id 标签id
     * @return 操作结果
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id){
        if (!labelService.removeById(id)) {
            return Result.fail("删除失败-_-!");
        }
        return Result.ok();
    }

    /**
     * 批量删除标签
     * @param ids 标签id
     * @return 操作结果
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody String[] ids) {
        if (labelService.removeByIds(Arrays.asList(ids))) {
            return Result.ok();
        }
        return Result.fail("操作失败-_-!");
    }

    /**
     * 修改标签
     * @param label 标签
     * @return 修改结果
     */
    @PutMapping("/update")
    public Result update(@RequestBody Label label) {
        if (!labelService.updateById(label)) {
            return Result.fail("修改失败-_-!");
        }
        return Result.ok();
    }

    /**
     * 获取全部标签数量
     * @param name 查询关键词
     * @return 数量
     */
    @GetMapping("/count")
    public Result count(
            @RequestParam(value = "name", required = false) String name
    ) {
        Integer count;
        if (Objects.equals(name, "") || name == null) {
            count = labelService.count();
        } else {
            count = labelService.query().like("label_name", name).count();
        }
        return Result.ok(count);
    }

    /**
     * 管理员/游客条件查询标签
     * @param current 当前页
     * @param size 页大小
     * @param name 查询关键词
     * @return 标签集
     */
    @GetMapping("/scan/list")
    public Result adminQuery(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        Page<Label> page;
        Integer count;
        if (Objects.equals(name, "") || name == null) {
            page = labelService.query()
                    .page(new Page<>(current, size));
            count = labelService.count();
        } else {
            page = labelService.query()
                    .like("label_name", name)
                    .page(new Page<>(current, size));
            count = labelService.query().like("label_name", name).count();
        }
        List<Label> records = page.getRecords();
        return Result.ok(new PageBean<>(records, count));
    }

    /**
     * 用户查询所有标签
     * @param current 当前页
     * @param name 查询关键词
     * @return 标签集
     */
    @GetMapping("/list")
    public Result userQuery(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        UserDTO user = UserHolder.getUser();
        return labelService.userQueryInCondition(name, user.getId(), current, size);
    }
}